﻿using RpcClient;
using RpcHelper;
using RpcModel;
using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Gateway.Model;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Gateway.Service
{
        internal class VerConfigService : IVerConfigService
        {
                private readonly IVerConfigCollect _VerConfig;
                private readonly IServerTypeCollect _ServerType;

                public VerConfigService (IVerConfigCollect ver, IServerTypeCollect type)
                {
                        this._ServerType = type;
                        this._VerConfig = ver;
                }
                public long Add (VerConfigDatum add)
                {
                        this._VerConfig.CheckIsRepeat (add.RpcMerId, add.SystemTypeId, add.VerNum);
                        return this._VerConfig.Add (add);
                }
                public long Copy (long id, long ver)
                {
                        ServiceVerConfig config = this._VerConfig.Get (id);
                        this._VerConfig.CheckIsRepeat (config.RpcMerId, config.SystemTypeId, ver);
                        return _VerConfig.Copy (config, ver);
                }
                public void Delete (long id)
                {
                        ServiceVerConfig config = this._VerConfig.Get (id);
                        this._VerConfig.Delete (config);
                }

                public ServiceVerConfig Get (long id)
                {
                        return this._VerConfig.Get (id);
                }

                public BasicVerConfig[] Query (VerQueryParam query, IBasicPage paging, out long count)
                {
                        BasicServiceVer[] vers = this._VerConfig.Query (query, paging, out count);
                        if (vers.Length == 0)
                        {
                                return null;
                        }
                        ServerType[] types = this._ServerType.Gets (vers.Distinct (a => a.SystemTypeId));
                        return vers.ConvertMap<BasicServiceVer, BasicVerConfig> ((a, b) =>
                        {
                                string ver = a.VerNum.ToString ().PadLeft (9, '0');
                                b.Ver = ver.Insert (3, ".").Insert (7, ".");
                                b.SystemType = types.Find (c => c.Id == a.SystemTypeId, c => c.SystemName);
                                return b;
                        });
                }
                public void SetIsEnable (long verId, bool isEnable)
                {
                        ServiceVerConfig config = this._VerConfig.Get (verId);

                        this._VerConfig.SetIsEnable (config, isEnable);
                }
                public void Set (long verId, ServiceVerSet set)
                {
                        ServiceVerConfig config = this._VerConfig.Get (verId);

                        this._VerConfig.Set (config, set);
                }
        }
}
